package com.ppch.wuwamanus.controller;

import com.ppch.wuwamanus.common.BaseResponse;
import com.ppch.wuwamanus.common.ResultUtils;
import com.ppch.wuwamanus.config.ImageUrlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Package:WuWaManus
 * ClassName:ImageUrlController
 *
 * @Author 泡泡茶壶
 * @Create 2025/9/19 21:36
 * @Version 1.0
 * Description:
 * 该控制器用于获取抽卡分析结果中的武器图片URL以及角色图片URL，便于前端展示
 */
@RestController
@RequestMapping("/image/url")
public class ImageUrlController {

    /**
     * 根据《鸣潮》中的角色名称获取对应的图片 URL 链接
     * @param imgResourcesNameList 资源名称集合
     */
    @PostMapping(value = "/character")
    public BaseResponse<Map<String, String>> getCharacterURL(@RequestBody List<String> imgResourcesNameList) {
        // 处理空输入
        if (imgResourcesNameList == null || imgResourcesNameList.isEmpty()) {
            return ResultUtils.success(Collections.emptyMap());
        }
        Map<String,String> resultMap = new LinkedHashMap<>();
        ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
        Map<String, String> characterURLMap = imageUrlUtils.getCharacterURL();
        for (String resourceName : imgResourcesNameList) {
            // 跳过无效Key
            if (characterURLMap.containsKey(resourceName)) {
                resultMap.put(resourceName, characterURLMap.get(resourceName));
            }
        }
        return ResultUtils.success(resultMap);
    }

    /**
     * 根据《鸣潮》中的武器名称获取对应的图片 URL 链接
     * @param imgResourcesNameList 资源名称集合
     */
    @PostMapping(value = "/weapon")
    public BaseResponse<Map<String, String>> getWeaponURL(@RequestBody List<String> imgResourcesNameList) {
        // 处理空输入
        if (imgResourcesNameList == null || imgResourcesNameList.isEmpty()) {
            return ResultUtils.success(Collections.emptyMap());
        }
        Map<String,String> resultMap = new LinkedHashMap<>();
        ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
        Map<String, String> weaponURLMap = imageUrlUtils.getWeaponURL();
        for (String resourceName : imgResourcesNameList) {
            // 跳过无效Key
            if (weaponURLMap.containsKey(resourceName)) {
                resultMap.put(resourceName, weaponURLMap.get(resourceName));
            }
        }
        return ResultUtils.success(resultMap);
    }

}
